package com.borened.common.core.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Treeselect树结构展示类(对应前端ElmentUi中的el-tree需要的数据结构)
 *
 * @author cch
 */
@Data
public class TreeSelectVO extends BaseVO {

    private static final long serialVersionUID = -3113502972416579139L;

    /**
     * 节点ID
     */
    private String id;

    /**
     * 节点名称
     */
    private String label;

    /**
     * 节点名称
     */
    private String name;

    /**
     * 子节点
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<TreeSelectVO> children;

    public TreeSelectVO() {

    }

    /**
     * 构建前端树形下拉对象
     *
     * @param [tree] 可树化的 树形数据对象
     */
    public <N extends Treeifyable<N>> TreeSelectVO(Treeifyable<N> tree) {
        this.id = tree.getNodeId();
        this.label = tree.getNodeName();
        this.name = tree.getNodeName();
        if (tree.getChildNodes() != null) {
            this.children = tree.getChildNodes().stream().map(TreeSelectVO::new).collect(Collectors.toList());
        }
    }
}
